import NvsData from '@/js/NvsData'
import NvData from '@/js/NvData'
import { ElMessage } from 'element-plus'
// import nvsDataUtils from '@/js/NvsDataUtils'
// import ConstDef from '@/js/ConstDef'

export const NvsDataCreator = () => {
  function createNvData (nvs, nv, nd, layout) {
    return new NvData(nvs, nv, nd, layout)
  }

  function createNvsData (nvs, nd, layout) {
    if (nvs == null) return null;
    return new NvsData(nvs, nd, layout)
  }

  function createChildNvsData(parentNv, childNd, childLayout) {
    if (parentNv == null) return null;
    const childNvs = parentNv[childNd.ndName];
    return new NvsData(childNvs, childNd, childLayout);
  }

  function getChildNvs (nv, ndName) {
    if (nv == null) { return null; } else return nv[ndName]
  }

  function getChildNd(nd, ndName) {
    if (nd.cursive && nd.ndName === ndName) { return nd; }
    for (const child of nd.children) {
      if (child.ndName === ndName) { return child; }
    }
    ElMessage({
      type: 'error',
      message: '在nd下在不到子Nd:' + ndName + " ndName为" + nd.ndName
    })
    return null;
  }
  function isNvLayout () {
    return true;
  }
  return {
    createNvData,
    createNvsData,
    createChildNvsData,
    getChildNvs,
    getChildNd,
    isNvLayout
  }
}
